//----------------------------------Include-------------------------------------
#include "mcu.h"
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
#define _TIMER_UNIT              (TIMG6)    //3507 inc  0/6/7/8/12
#define _INT_IRQn                (TIMG6_INT_IRQn)
//------------------------------------------------------------------------------

//--------------------------------Static Var------------------------------------
static uint32_t    src_clk;
static uint32_t    clk_div;
static uint32_t    req_freqHz;
static uint32_t    period_cnt;
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
//------------------------------------------------------------------------------

//------------------------------------ISR---------------------------------------
void TIMG6_IRQHandler(void)
{
  if( DL_TimerG_getPendingInterrupt(_TIMER_UNIT) == DL_TIMER_IIDX_ZERO ){
#if 1
    static unsigned int cnt;
    if(++cnt >= 1000){
      cnt = 0;
      //mcu_led_toggle();
    }
#endif
  }
}
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
void mcu_init_tim_g6(void)
{
//periph clock
  DL_TimerG_enablePower(_TIMER_UNIT);
  
//timer base
  src_clk    = mcu_get_mfclk();
  clk_div    = 1;
  req_freqHz = 1000;
  period_cnt = (src_clk/clk_div)/req_freqHz;
  
  //timerClkFreq = (timerClkSrc / (timerClkDivRatio * (timerClkPrescale + 1)))
  static DL_TimerG_ClockConfig    clk_config;
  clk_config.clockSel    = DL_TIMER_CLOCK_MFCLK;
  clk_config.divideRatio = DL_TIMER_CLOCK_DIVIDE_1;
  clk_config.prescale    = clk_div -1;
  DL_TimerG_setClockConfig(_TIMER_UNIT, (DL_TimerG_ClockConfig *) &clk_config);
  
  //Timer load value (where the counter starts from) is calculated as (timerPeriod * timerClockFreq) - 1
  static DL_TimerG_TimerConfig    tim_config;
  tim_config.period     = period_cnt -1;
  tim_config.timerMode  = DL_TIMER_TIMER_MODE_PERIODIC;
  tim_config.startTimer = DL_TIMER_STOP;
  DL_TimerG_initTimerMode(_TIMER_UNIT, (DL_TimerG_TimerConfig *) &tim_config);
  
//interrupt
  DL_TimerG_enableInterrupt(_TIMER_UNIT , DL_TIMERG_INTERRUPT_ZERO_EVENT);
  NVIC_EnableIRQ(_INT_IRQn);
  
//enable
  DL_TimerG_enableClock(_TIMER_UNIT);
  DL_TimerG_startCounter(_TIMER_UNIT);
}
//------------------------------------------------------------------------------


//------------------------------------EOF---------------------------------------





